# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => 'b7a6b292e904119a2949cd670aea7515'

  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password

  session :session_key => '_linknovel_session'  #TODO what this mean?
  
  before_filter :check_cookie,:set_locale
  # before_filter :set_locale 
  def set_locale 
	# if params[:locale] is nil then I18n.default_locale will be used  
	I18n.locale = params[:locale] if params[:locale]
  end 

  def check_cookie
    #session[:original_uri] = request.request_uri
    if cookies and cookies[:userapp_login] and cookies[:userapp_login_pass]
      @login_user = User.find_by_name(cookies[:userapp_login])
      return unless @login_user
      cookies_hash = Digest::MD5.hexdigest(cookies[:userapp_login_pass] + @login_user.hashed_password)
      if @login_user.cookies_hash == cookies_hash
        session[:user_id] = @login_user.id
      else
        session[:user_id] = nil
        @login_user = nil
      end
    end
  end
end
